import React, { useState, useEffect } from "react";
import { useSelector } from "react-redux";
import { useNavigate } from "react-router-dom";
// 登录才能访问，否则跳转到登录页面

function checklogin(Com:any) {
  function Hoc(props:any) {
    
    //
    const navigate = useNavigate();
    // console.log(navigate, "nav");

    // 登的状态获取到了
    const isLogin = useSelector((state:any) => state.user.cookie);
    // console.log(isLogin, "是否登录");
    useEffect(() => {
      if (isLogin) {
        console.log("已经登录");
      } else {
        // 跳转到登录页面
        console.log("为执行？");
        navigate("/login");
      }
    });
    return (
      <>
        <Com {...props}></Com>
      </>
    );
  }

  return Hoc;
}

export default checklogin;

// checklogin()  === Hoc
